package com.google.code.shardbatis.sharding.strategy;

import com.google.code.shardbatis.bean.UserBean;
import com.google.code.shardbatis.sharding.exception.ShardingRuntimeException;
import com.google.code.shardbatis.sharding.key.GhostKeyParser;
import com.google.code.shardbatis.strategy.ShardStrategy;
import org.apache.commons.beanutils.BeanUtilsBean2;
import org.apache.ibatis.mapping.SqlCommandType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;

public class UserShardStrategyImpl implements ShardStrategy {
    private static final Logger logger = LoggerFactory.getLogger(UserShardStrategyImpl.class);

    @Override
    public String getTargetTableName(SqlCommandType commandType, String baseTableName, Object params, String mapperId) {
        if (params != null) {
            try {
                long datacenterId = -1L;
                switch (commandType) {
                    case INSERT: {
                        if (params instanceof UserBean) {
                            String val = BeanUtilsBean2.getInstance().getSimpleProperty(params, "clientSn");
                            datacenterId = GhostKeyParser.getDatacenterId(Long.parseLong(val));
                        }
                    }
                    case UPDATE: {
                        if (params instanceof UserBean) {
                            String val = BeanUtilsBean2.getInstance().getSimpleProperty(params, "id");
                            datacenterId = GhostKeyParser.getDatacenterId(Long.parseLong(val));
                        }
                    }
                    break;
                    case SELECT:
                        if (params instanceof Integer || params instanceof Long) {
                            datacenterId = GhostKeyParser.getDatacenterId(Long.valueOf(params.toString()));
                        } else if (params instanceof UserBean) {
                            String val = BeanUtilsBean2.getInstance().getSimpleProperty(params, "id");
                            datacenterId = GhostKeyParser.getDatacenterId(Long.parseLong(val));
                        }
                        break;
                    case DELETE:
                        if (params instanceof Integer || params instanceof Long) {
                            datacenterId = GhostKeyParser.getDatacenterId(Long.valueOf(params.toString()));
                        } else if (params instanceof UserBean) {
                            String val = BeanUtilsBean2.getInstance().getSimpleProperty(params, "id");
                            datacenterId = GhostKeyParser.getDatacenterId(Long.parseLong(val));
                        }
                        break;
                    default:
                        datacenterId = -1L;
                        break;
                }
                if (datacenterId < 0L) {
                    throw new ShardingRuntimeException("UserShardStrategyImpl.getTargetTableName分表异常");
                }
                return baseTableName + "_" + datacenterId;
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                logger.error("*****UserShardStrategyImpl.getTargetTableName异常:" + e.getMessage(), e);
                throw new ShardingRuntimeException("UserShardStrategyImpl.getTargetTableName异常:" + e.getMessage());
            }
        }
        return null;
    }
}
